﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SAM.Configuration.Domain
{
    public abstract class AbstractConfigurationBuild:IReadAndWrite
    {
        string baseFilePath;

        public AbstractConfigurationBuild()
        {
            this.baseFilePath = System.IO.Directory.GetCurrentDirectory();
        }

        public AbstractConfigurationBuild(string baseFilePath)
        {
            this.baseFilePath = baseFilePath;
        }

        protected string FullPath(string fileName)
        {
            return System.IO.Path.Combine(this.baseFilePath, fileName);
        }

        protected bool ExistsFile(string filePath)
        {
            return System.IO.File.Exists(filePath);
        }

        protected string ReadFile(string filePath, System.Text.Encoding encoding)
        {
            return System.IO.File.ReadAllText(filePath, encoding);
        }

        protected void CreateFile(string filePath, string data, Encoding encoding)
        {
            System.IO.File.WriteAllText(filePath, data, encoding);
        }

        /// <summary>
        /// 读取文件格式为UTF-8的所有内容
        /// </summary>
        /// <param name="filePath">文件全路径</param>
        /// <returns></returns>
        protected string ReadFile(string filePath)
        {
            return System.IO.File.ReadAllText(filePath, System.Text.Encoding.UTF8);
        }

        public abstract T Read<T>(string fileName);
        public abstract void Write<T>(string fileName, T data);
    }
}
